using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;

public class VActHandlerBase
{
    public VSBasicBehaviour bb;
    public VActProcessor processor;
    public virtual void Init() { }
    public virtual void PrepareFrame(Playable playable = default, FrameData info = default) { }
    public virtual void Clear() { }
}
#region Animation
[Serializable]
public class VAct_AnimationHandler_Data
{
    public AnimationClip clip;
}

public class VAct_AnimationHandler : VActHandlerBase
{
    VAct_AnimationHandler_Data data;

    public VAct_AnimationHandler(VSBasicBehaviour basicBehaviour)
    {
        this.bb = basicBehaviour;
        data = bb.AnimationHandler_Data;
        processor = bb.processor;
    }
    private VS_AnimationGraph graph => processor.VS_AnimationGraph;
    private bool set = false;
    //这个预览还是改成原来的版本,
    public override void Init()
    {
        if (!set)
        {
            processor.DirectorSetClip(data.clip);
            graph.Stop();
            set = true;
        }
    }

    public override void PrepareFrame(Playable playable = default, FrameData info = default)
    {
        base.PrepareFrame();
        double curTime = playable.GetTime();
        graph?.Evaluate(1, curTime);
    }
    /// <summary>
    /// 这里有个问题就是可能不会手动关闭
    /// </summary>
    public override void Clear()
    {
        if (set)
        {
            processor.DirectorSetClip();
            graph.PlayGraph();
            set = false;
        }
    }
}

#endregion

#region Vacancy
[Serializable]
public class VAct_VacancyHandler_Data
{
    public bool inputEnable;
    [TableList]
    public List<VVacancyActMapper> fliterActs;
}
public class VAct_VacancyHandler : VActHandlerBase
{
    VAct_VacancyHandler_Data data;
    public VAct_VacancyHandler(VSBasicBehaviour basicBehaviour)
    {
        this.bb = basicBehaviour;
        data = bb.VacancyHandler_Data;
        processor = bb.processor;
    }
    private bool set;
    public override void Init()
    {
        if (!set)
        {
            processor.onEnterVacancyStringArgs?.Invoke(data.fliterActs);
            set = true;
        }
    }

    public override void Clear()
    {
        if (set)
        {
            processor.onEnterVacancyStringArgs?.Invoke(null);
            set = false;
        }
    }

}

#endregion

#region  Entity
[Serializable]
public class VAct_EntityHandler_Data
{
    [LabelText("附带技能效果code")] public string AttachSkillEffect;
    [LabelText("独立生命周期")] public bool extraLiveTime = false;
    [LabelText("全局坐标系")] public bool gloable = false;
    public GameObject prefab;
    /// <summary>
    /// 用来提供相对路径
    /// </summary>
    public Transform rootTransform;

    public GameObject runtimeGameObject { get; set; }
    public Transform runtimeParent { get; set; }
}

public class VAct_EntityHandler : VActHandlerBase
{
    VAct_EntityHandler_Data data;
    public VAct_EntityHandler(VSBasicBehaviour basicBehaviour)
    {
        this.bb = basicBehaviour;
        data = bb.EntityHandler_Data;
        processor = bb.processor;
    }
    private bool set;


    public override void Init()
    {
        if (!set)
        {
            if (data.prefab == null) return;
            GetParentByPath(bb.bindTransformPath, processor.transform);
            data.runtimeGameObject = GameObject.Instantiate(data.prefab);
            if (data.runtimeParent != null)
            {
                if (data.gloable)
                {
                    data.runtimeGameObject.transform.position = data.runtimeParent.transform.position;
                }
                else
                {
                    data.runtimeGameObject.transform.SetParent(data.runtimeParent);
                    //可能需要配置偏移量 , 并且支持local坐标和world坐标
                    data.runtimeGameObject.transform.localPosition = Vector3.zero;
                    data.runtimeGameObject.transform.localRotation = Quaternion.identity;
                }
            }
            else
            {
                data.runtimeGameObject.transform.SetParent(processor.transform);
            }

            if (!string.IsNullOrEmpty(data.AttachSkillEffect))
            {
                //尝试获取技能特效并注册各种回调
                var effect = data.runtimeGameObject.AddComponent<VActSkillEffect>();
                effect.LoadEffect(processor,data.AttachSkillEffect);
            }
            set = true;
        }
    }
    public override void PrepareFrame(Playable playable = default, FrameData info = default)
    {

    }

    public override void Clear()
    {
        if (set)
        {
            if (data.prefab == null) return;
            if (data.runtimeGameObject != null && !data.extraLiveTime)
            {
                GameObject.DestroyImmediate(data.runtimeGameObject);
            }
            set = false;
        }
    }

    private void GetParentByPath(string path, Transform root)
    {

        if (path == "") data.runtimeParent = root;
        else data.runtimeParent = root.Find(path);
    }
}

#endregion



#region  Interact
[Serializable]
public class VAct_InteractHandler_Data
{
    [LabelText("交互函数名")] public string methodName;
    //TODO:等待外部实现交互参数传入
    public Func<bool> CatchMethodByName() => null;
}

public class VAct_InteractHandler : VActHandlerBase
{
    VAct_InteractHandler_Data data;
    public VAct_InteractHandler(VSBasicBehaviour basicBehaviour)
    {
        this.bb = basicBehaviour;
        data = bb.InteractHandler_Data;
        processor = bb.processor;
    }
    private bool set;


    public override void Init()
    {
        if (!set)
        {
            processor.onInteract = data.CatchMethodByName(); // 这里就需要外部来创建闭包然后通过全局索引来绑定了,实现逻辑的分离
            set = true;
        }
    }
    //这里写判断逻辑
    public bool CheckNameMimi() => Input.GetKey(KeyCode.V);
    public override void Clear()
    {
        if (set)
        {
            processor.onInteract = null;
            set = false;
        }
    }
}


#endregion

#region  Control
[Serializable]
public class VAct_ControlHandler_Data
{
    [LabelText("最大存活时间")]
    public float maxLivetime;

    public string NextActName;
    public VAct GetAct() => null;
    //这里是不是可以根据参数来选择流向, 去下一个act

    //甚至承接上一个Act的数据

    //结束时流向

    //结束时传参数id

    //开始时传参数

}

public class VAct_ControlHandler : VActHandlerBase
{
    VAct_ControlHandler_Data data;
    public VAct_ControlHandler(VSBasicBehaviour basicBehaviour)
    {
        this.bb = basicBehaviour;
        data = bb.SkillControlHandler_Data;
        processor = bb.processor;
    }
    private bool set;

    public override void Init()
    {
        if (!set)
        {
            var shutdownObserver = data.maxLivetime.Delay(() =>
            {
                processor.onAskEnterAct(data.NextActName);
                Debug.Log("成功关闭进程");
            }
            );//得把这个Observer注册在某个上面,然后observer伴随这个生命周期,
            shutdownObserver.RelayOn(processor.GetCurAct.actObserver);
            set = true;
        }
    }
    public override void PrepareFrame(Playable playable = default, FrameData info = default)
    {
        // Debug.Log(playable.GetTime());
    }
    public override void Clear()
    {
        if (set)
        {
            set = false;
        }
    }
}
#endregion

#region  Audio
[Serializable]
public class VAct_AudioHandler_Data
{
    public AudioClip audioClip;
    public bool looping;
}

public class VAct_AudioHandler : VActHandlerBase
{
    VAct_AudioHandler_Data data;
    public VAct_AudioHandler(VSBasicBehaviour basicBehaviour)
    {
        this.bb = basicBehaviour;
        data = bb.AudioHandler_Data;
        processor = bb.processor;
    }
    private bool set;


    public override void Init()
    {
        if (!set)
        {
            set = true;
        }
    }
    public override void PrepareFrame(Playable playable = default, FrameData info = default)
    {

    }

    public override void Clear()
    {
        if (set)
        {

            set = false;
        }
    }
}
#endregion




#region  Template
// [Serializable]
// public class VAct_InteractHandler_Data
// {
// 
// }
// 
// public class VAct_InteractHandler : VActHandlerBase
// {
//     VAct_InteractHandler_Data data;
//     public VAct_InteractHandler(VSBasicBehaviour basicBehaviour)
//     {
//         this.bb = basicBehaviour;
//         data = bb.InteractHandler_Data;
//         processor = bb.processor;
//     }
//     private bool set;
// 
// 
//     public override void Init()
//     {
//         if (!set)
//         {
//             set = true;
//         }
//     }
//     public override void PrepareFrame(Playable playable = default, FrameData info = default)
//     {
// 
//     }
// 
//     public override void Clear()
//     {
//         if (set)
//         {
// 
//             set = false;
//         }
//     }
// }
#endregion



